#include "stdafx.h"
#include "MockProvider.h"
#include <assert.h>

#define NEWS_TEXT "MNMPROGX FAILED 6:36:59AM 8D19H"
#define URL "http://4fid06486:8080/composite_lavalamp_ipa.jsp"

CMockProvider::CMockProvider ()
{
	m_nMockNumber = -1;
	m_nCurItem = 0;
	for (int i = 0; i < MOCK_ITEMS; i++) {
		m_Items[i].text = NULL;
	}
}

CMockProvider::~CMockProvider ()
{
	if (m_nMockNumber < 0) {
		return;
	}
	
	for (int i = 0; i < MOCK_ITEMS; i++) {
		// in destructor, no need to set to NULL.
		free (m_Items[i].text);
	}
}

int CMockProvider::SetMockNumber(int nbr)
{
	assert (nbr>=0);
	m_nMockNumber = nbr;

	int i;
	for (i = 0; i < MOCK_ITEMS; i++) {
		m_Items[i].text = (char*)malloc(1024);
		if (!m_Items[i].text) {
			return -1;
		}
		sprintf(m_Items[i].text, "MOCK ID %d : Item ID %d -- %s.",
				m_nMockNumber, i, NEWS_TEXT);
		if (i/2 *2 == i) {
			m_Items[i].bkgrd = RGB(255, 0, 0);
			m_Items[i].fore = RGB (255, 255, 255);
		} else {
			m_Items[i].bkgrd = RGB(0, 255, 0);
			m_Items[i].fore = RGB(0, 0, 0);
		}
	}
	return 0;
}


void CMockProvider::GetNextNewsItem(NewsItem** ppItem)
{
	if (m_nCurItem >= MOCK_ITEMS) {
		*ppItem = NULL;
		m_nCurItem = 0;
		return;
	}
	
	*ppItem = &(m_Items[m_nCurItem ++]);
	return;
}

void CMockProvider::ReleaseNewsItem(NewsItem* pItem)
{
	// No need to release memory in this implementation.
	return;
}

void CMockProvider::Tick()
{
	// Not implemented.
	return;
}

size_t CMockProvider::GetURLSize()
{
	return (strlen(URL)+1);
}

int CMockProvider::GetURL(char* url_buf, size_t bufsize)
{
	if (bufsize < strlen(URL)+1) {
		return -1;
	}
	
	strcpy(url_buf, URL);
	return 0;
}
